﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AI.Steering
{
    /// <summary>
    /// 路径跟随
    /// </summary>
    public class SteeringForFollowPath : Steering
    {
        public enum PatrolMode
        {
            /// <summary>
            /// 单次
            /// </summary>
            Once,
            /// <summary>
            /// 循环
            /// </summary>
            Loop,
            /// <summary>
            /// 往返
            /// </summary>
            PingPong
        }
        private int index;
        public Transform[] wayPoints;
        public PatrolMode patrolMode;
        [Tooltip("巡逻到达距离")]
        public float patrolArrivalDistance = 1f;
        public override Vector3 GetForce()
        {
            if (Vector3.Distance(transform.position, wayPoints[index].position) <= patrolArrivalDistance)
            {
                //最后一个路点
                if (index == wayPoints.Length - 1)
                {
                    switch (patrolMode)
                    {
                        case PatrolMode.Once:
                            return Vector3.zero;//返回的实际力 为零
                        case PatrolMode.PingPong:
                            Array.Reverse(wayPoints);//数组反转
                            break;
                        default:
                            break;
                    }
                }
                //索引增加，对应Loop巡逻
                index = (index + 1) % wayPoints.Length;//取余好，周期性变化用
            }
            //wayPoints[index].position
            //计算期望力
            Vector3 expectForce = (wayPoints[index].position - transform.position).normalized * speed;
            //实际力 = 期望力 - 当前力//返回实际力
            return (expectForce - controller.currentForce) * weight;//权重越大，实际力到打最大越快
        }
    }
}

